package server.multiServlet;


import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;
import java.util.concurrent.*;

@Data
public class Service implements LifeCycle {
    private List<Connector> connector = Lists.newArrayList();

    private ContainerBase container = new ContainerBase();

    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public void init() {
        initThread();
        container.init();
        connector.forEach(conn -> {
            conn.setThreadPoolExecutor(threadPoolExecutor);
            conn.setContainerBase(container);
            conn.init();
        });

    }

    private void initThread() {

        // 定义一个线程池
        int corePoolSize = 10;
        int maximumPoolSize = 50;
        long keepAliveTime = 100L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();


        threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler
        );
    }
}
